package l1j.opqlo.NewSystem;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_Sound;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.utils.Random;

public class 刮刮樂 {

	private static final HashMap<Integer, String> _value = new HashMap<Integer, String>();
	private static final HashMap<Integer, Integer> _Moneyvalue = new HashMap<Integer, Integer>();
	private static final HashMap<String, Integer> _Moneycount = new HashMap<String, Integer>();
	private static 刮刮樂 _instance;
	private static final Logger _log = Logger.getLogger(刮刮樂.class.getName());

	public static 刮刮樂 getInstance() {
		if (_instance == null) {
			_instance = new 刮刮樂();
		}
		return _instance;
	}

	private int _betCount;
	private int _payCount;

	public 刮刮樂() {
		_value.put(0, "*");
		_value.put(1, "1");
		_value.put(2, "2");
		_value.put(3, "3");
		_value.put(4, "4");
		_value.put(5, "5");
		_value.put(6, "6");
		_value.put(7, "7");
		_value.put(8, "8");
		_value.put(9, "9");
		_Moneyvalue.put(0, 0);
		_Moneyvalue.put(1, 50);
		_Moneyvalue.put(2, 200);
		_Moneyvalue.put(3, 500);
		_Moneyvalue.put(4, 1500);
		_Moneyvalue.put(5, 4500);
		_Moneyvalue.put(6, 15000);
		_Moneyvalue.put(7, 30000);
		_Moneyvalue.put(8, 45000);
		_Moneyvalue.put(9, 90000);
		_Moneycount.put("0", 0);
		_Moneycount.put("50", 1);
		_Moneycount.put("200", 2);
		_Moneycount.put("500", 3);
		_Moneycount.put("1500", 4);
		_Moneycount.put("4500", 5);
		_Moneycount.put("15000", 6);
		_Moneycount.put("30000", 7);
		_Moneycount.put("45000", 8);
		_Moneycount.put("90000", 9);

	}

	public void addBetCount() {
		this._betCount += 30;
		StringBuffer log = new StringBuffer();
		log.append(" 總收入: ").append(this._betCount).append(" 總支出: ")
				.append(this._payCount).append(" 營收: ")
				.append(this._betCount - this._payCount);
		System.out.println(log.toString());
		log.delete(0, log.length());
		log = null;
	}

	public void addPayCount(final int i) {
		this._payCount += i;
		StringBuffer log = new StringBuffer();
		log.append(" 總收入: ").append(this._betCount).append(" 總支出: ")
				.append(this._payCount).append(" 營收: ")
				.append(this._betCount - this._payCount);
		System.out.println(log.toString());
		log.delete(0, log.length());
		log = null;
	}

	/*
	 * private int getRandomInt(L1PcInstance pc){ int r = Random.nextInt(9)+1;
	 * int i = pc.get中獎次數(); if(r ==7 ){ while(i>0){ System.out.println(i); r =
	 * Random.nextInt(9)+1; if(r !=7){ break; } i--; } } return r; }
	 */
	public boolean checkOver(final L1PcInstance pc) {

		String[] msg = pc.get刮刮樂號碼();
		if (msg == null) {
			_log.log(Level.SEVERE, pc.getName() + " 異常的領獎動作。");
			return false;
		}
		for (int i = 0; i < 9; i++) {
			if (msg[i].equalsIgnoreCase("*")) {
				return false;
			}
		}
		msg = null;
		return true;
	}

	private int getRandomInt(L1PcInstance pc) {
		int r = 0;
		final int chance = Random.nextInt(100) + 1;
		if (chance <= 4) {
			r = 7;
		} else if (chance <= 16) {
			r = 6;
		} else if (chance <= 28) {
			r = 5;
		} else if (chance <= 40) {
			r = 4;
		} else if (chance <= 52) {
			r = 3;
		} else if (chance <= 64) {
			r = 2;
		} else if (chance <= 76) {
			r = 1;
		} else if (chance <= 88) {
			r = 8;
		} else {
			r = 9;
		}
		if (r == 7) {
			pc.sendPackets(new S_Sound(209));
		} else {
			pc.sendPackets(new S_Sound(210));
		}

		return r;
	}

	public int get中獎次數(final String s) {
		return _Moneycount.get(s);
	}

	public int get中獎金額(final int i) {
		return _Moneyvalue.get(i);
	}

	/** opqlo 儲存刮刮樂收支 */
	public void saveTheTotalCount() {
		final StringBuffer log = new StringBuffer();
		log.append(new Timestamp(System.currentTimeMillis())).append(" 總收入: ")
				.append(this._betCount).append(" 總支出: ").append(this._payCount)
				.append(" 營收: ").append(this._betCount - this._payCount);
		各項記錄輸出.LogSave(29, log.toString());
		System.out.println("刮刮樂資料儲存完畢.....");

	}

	public String[] startGame(L1PcInstance pc, final int value) {
		String[] msg = pc.get刮刮樂號碼();
		if (value == -1) {
			if (msg == null) {
				msg = new String[10];
			}
			for (int i = 0; i < 9; i++) {
				msg[i] = _value.get(0);
			}
			pc.set刮刮樂號碼(msg);
		} else {
			if (msg == null) {
				pc.sendPackets(new S_SystemMessage("尚未購買彩票，異常動作已被系統紀錄。"));
				_log.info(pc.getName() + "疑似修改刮刮樂對話檔。");
				// pc.getNetConnection().close();
				return null;
			}
			if (msg[value].equalsIgnoreCase("*")) {
				msg[value] = _value.get(this.getRandomInt(pc));
				if (msg[value].equalsIgnoreCase("7")) {
					pc.add中獎次數(1);
				}
			} else {
				pc.sendPackets(new S_SystemMessage("這個地方已經刮過了。"));
			}
		}
		msg[9] = String.valueOf(_Moneyvalue.get(pc.get中獎次數()));
		return msg;
		// pc.sendPackets(new S_NPCTalkReturn(pc.getId(), "haha", msg));
	}
}
